package edu.its.dreamteam.web.pages.manager;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.DraftPlayer;
import edu.its.dreamteam.daf.beans.Team;
import edu.its.dreamteam.daf.dao.IDraftPlayerDao;
import edu.its.dreamteam.daf.dao.ITeamDao;
import edu.its.dreamteam.web.base.SecureBasePage;

public class EditTeamPlayer extends SecureBasePage{
	@Inject
	private IDraftPlayerDao draftPlayerDao;
	@Inject
	private ITeamDao teamDao;
	
	private List<DraftPlayer> draftPlayerList;
	private DraftPlayer draftPlayer;
	private Team team;
	@Retain
	private BeanModel draftPlayerModel;
	@Inject
	private BeanModelSource draftPlayerModelSource;
    @Inject
    private ComponentResources resources;
    
    @Retain
	private BeanModel draftPlayerGoalKeeperModel;
    @Retain
	private BeanModel draftPlayerDefenderModel;
    @Retain
	private BeanModel draftPlayerMidfielderModel;
    @Retain
	private BeanModel draftPlayerFowardModel;
    
    private List<DraftPlayer> draftPlayerGoalKeeperList;
	private List<DraftPlayer> draftPlayerDefenderList;
	private List<DraftPlayer> draftPlayerMidfielderList;
	private List<DraftPlayer> draftPlayerFowardList;
    
	public List<DraftPlayer> getDraftPlayerList() {
		if(draftPlayerList==null){
			draftPlayerList = new ArrayList<DraftPlayer>();
			draftPlayerList = getDraftPlayerDao().queryForList("\"id_team\" ='"+getTeam().getIdTeam()+"' AND \"status_draft\"='1' AND \"play\"='0'" , "");
		}
		return draftPlayerList;
	}
	public void setDraftPlayerList(List<DraftPlayer> draftPlayerList) {
		this.draftPlayerList = draftPlayerList;
	}
	
	
	public Team getTeam() {
		if(team==null){
			team = (Team) getTeamDao().queryForBaseBean("\"username\" ='"+getVisit().getUsername()+"' ");
		}
		return team;
	}
	public void setTeam(Team team) {
		this.team = team;
	}
	public DraftPlayer getDraftPlayer() {
		return draftPlayer;
	}
	public void setDraftPlayer(DraftPlayer draftPlayer) {
		this.draftPlayer = draftPlayer;
	}
	
	public IDraftPlayerDao getDraftPlayerDao() {
		return draftPlayerDao;
	}
	
	public ITeamDao getTeamDao() {
		return teamDao;
	}
	public BeanModel getDraftPlayerModel() {
		return draftPlayerModel;
	}
	
	public BeanModel getDraftPlayerGoalKeeperModel() {
		return draftPlayerGoalKeeperModel;
	}
	public BeanModel getDraftPlayerDefenderModel() {
		return draftPlayerDefenderModel;
	}
	public BeanModel getDraftPlayerMidfielderModel() {
		return draftPlayerMidfielderModel;
	}
	public BeanModel getDraftPlayerFowardModel() {
		return draftPlayerFowardModel;
	}
	public List<DraftPlayer> getDraftPlayerGoalKeeperList() {
		if(draftPlayerGoalKeeperList==null){
			draftPlayerGoalKeeperList = getDraftPlayerDao().getDraftPlayerByPosition("POST-00001", "1", getTeam().getIdTeam(),"1");
		}
		return draftPlayerGoalKeeperList;
	}
	public List<DraftPlayer> getDraftPlayerDefenderList() {
		if(draftPlayerDefenderList==null){
			draftPlayerDefenderList = getDraftPlayerDao().getDraftPlayerByPosition("POST-00002", "1", getTeam().getIdTeam(),"1");
		}
		return draftPlayerDefenderList;
	}
	public List<DraftPlayer> getDraftPlayerMidfielderList() {
		if(draftPlayerMidfielderList==null){
			draftPlayerMidfielderList = getDraftPlayerDao().getDraftPlayerByPosition("POST-00003", "1", getTeam().getIdTeam(),"1");
		}
		return draftPlayerMidfielderList;
	}
	public List<DraftPlayer> getDraftPlayerFowardList() {
		if(draftPlayerFowardList==null){
			draftPlayerFowardList = getDraftPlayerDao().getDraftPlayerByPosition("POST-00004", "1", getTeam().getIdTeam(),"1");
		}
		return draftPlayerFowardList;
	}
	
	void pageLoaded(){
    	draftPlayerModel = draftPlayerModelSource.create(DraftPlayer.class, false, resources);    	
    	draftPlayerModel.remove("idDraft","statusDraft","idTeam","dateDraft");
    	draftPlayerModel.get("idPlayer").label("Player");
    	draftPlayerModel.add("club",null).label("Club");
    	draftPlayerModel.add("position",null).label("Position");
    	draftPlayerModel.get("bid").label("Current Price");
    	draftPlayerModel.add("actions", null).label("Play");
    	draftPlayerModel.reorder("idPlayer","club","position");
    	
    	
    	draftPlayerGoalKeeperModel = draftPlayerModelSource.create(DraftPlayer.class, false, resources);
    	draftPlayerGoalKeeperModel.remove("idDraft","statusDraft","idTeam","dateDraft");
    	draftPlayerGoalKeeperModel.get("idPlayer").label("Player");
    	draftPlayerGoalKeeperModel.add("club",null).label("Club");
    	draftPlayerGoalKeeperModel.add("position",null).label("Position");
    	draftPlayerGoalKeeperModel.get("bid").label("Current Price");
    	draftPlayerGoalKeeperModel.add("actions", null).label("Play");
    	draftPlayerGoalKeeperModel.reorder("idPlayer","club","position");
    	
    	draftPlayerDefenderModel = draftPlayerModelSource.create(DraftPlayer.class, false, resources);
    	draftPlayerDefenderModel.remove("idDraft","statusDraft","idTeam","dateDraft");
    	draftPlayerDefenderModel.get("idPlayer").label("Player");
    	draftPlayerDefenderModel.add("club",null).label("Club");
    	draftPlayerDefenderModel.add("position",null).label("Position");
    	draftPlayerDefenderModel.get("bid").label("Current Price");
    	draftPlayerDefenderModel.add("actions", null).label("Play");
    	draftPlayerDefenderModel.reorder("idPlayer","club","position");
    	
    	draftPlayerMidfielderModel = draftPlayerModelSource.create(DraftPlayer.class, false, resources);
    	draftPlayerMidfielderModel.remove("idDraft","statusDraft","idTeam","dateDraft");
    	draftPlayerMidfielderModel.get("idPlayer").label("Player");
    	draftPlayerMidfielderModel.add("club",null).label("Club");
    	draftPlayerMidfielderModel.add("position",null).label("Position");
    	draftPlayerMidfielderModel.get("bid").label("Current Price");
    	draftPlayerMidfielderModel.add("actions", null).label("Play");
    	draftPlayerMidfielderModel.reorder("idPlayer","club","position");
    	
    	draftPlayerFowardModel = draftPlayerModelSource.create(DraftPlayer.class, false, resources);
    	draftPlayerFowardModel.remove("idDraft","statusDraft","idTeam","dateDraft");
    	draftPlayerFowardModel.get("idPlayer").label("Player");
    	draftPlayerFowardModel.add("club",null).label("Club");
    	draftPlayerFowardModel.add("position",null).label("Position");
    	draftPlayerFowardModel.get("bid").label("Current Price");
    	draftPlayerFowardModel.add("actions", null).label("Play");
    	draftPlayerFowardModel.reorder("idPlayer","club","position");
    }	
    
	void onSuccessFromDraftPlayerForm(String idDraft){
		DraftPlayer draftPlayerManager = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(idDraft);
		if(draftPlayerManager.getPlayer().getIdPosition().equals("POST-00001")){
			if(getDraftPlayerGoalKeeperList().size()<2){
				draftPlayerManager.setPlay("1");
				getDraftPlayerDao().update(draftPlayerManager);
			}
		}else if (draftPlayerManager.getPlayer().getIdPosition().equals("POST-00002")){
			if(getDraftPlayerDefenderList().size()<=draftPlayerManager.getTeam().getFormation().getTotalDF()){
				draftPlayerManager.setPlay("1");
				getDraftPlayerDao().update(draftPlayerManager);
			}
		}else if (draftPlayerManager.getPlayer().getIdPosition().equals("POST-00003")){
			if(getDraftPlayerMidfielderList().size()<=draftPlayerManager.getTeam().getFormation().getTotalMF()){
				draftPlayerManager.setPlay("1");
				getDraftPlayerDao().update(draftPlayerManager);
			}
		}else if (draftPlayerManager.getPlayer().getIdPosition().equals("POST-00004")){
			if(getDraftPlayerFowardList().size()<=draftPlayerManager.getTeam().getFormation().getTotalFW()){
				draftPlayerManager.setPlay("1");
				getDraftPlayerDao().update(draftPlayerManager);
			}
		}
		
		
	}
	
	void onSuccessFromDraftPlayerGoalKeeperForm(String idDraft){
		DraftPlayer draftPlayerManager = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(idDraft);
		draftPlayerManager.setPlay("0");
		getDraftPlayerDao().update(draftPlayerManager);
	}
	
	void onSuccessFromDraftPlayerDefenderForm(String idDraft){
		DraftPlayer draftPlayerManager = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(idDraft);
		draftPlayerManager.setPlay("0");
		getDraftPlayerDao().update(draftPlayerManager);
	}
	
	void onSuccessFromDraftPlayerMidfielderForm(String idDraft){
		DraftPlayer draftPlayerManager = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(idDraft);
		draftPlayerManager.setPlay("0");
		getDraftPlayerDao().update(draftPlayerManager);
	}
	
	void onSuccessFromDraftPlayerFowardForm(String idDraft){
		DraftPlayer draftPlayerManager = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(idDraft);
		draftPlayerManager.setPlay("0");
		getDraftPlayerDao().update(draftPlayerManager);
	}
	
    
}
